﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.PowerShell;

using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Cmdlet;

namespace Community.SharePoint.PowerShell
{
    /// <summary>
    /// SharePoint PowerShell Community Kit
    /// Remove-SPEnterpriseSearchQueryScopeDisplayGroupScope
    /// This cmdlet creates removes a scope from a given display group given 
    /// ScopeDisplayGroupPipeBind and ScopePipeBind
    /// Example (Removes the "All Sites" scope from the "Search Dropdown" display group): 
    /// $searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
    /// $group = Get-SPEnterpriseSearchQueryScopeDisplayGroup -Name "Search Dropdown" -url http://sp2010 -SearchApplication $searchapp
    /// $scope = Get-SPEnterpriseSearchQueryScope -Identity "All Sites" -SearchApplication $searchapp
    /// Remove-SPEnterpriseSearchQueryScopeDisplayGroupScope -ScopeDisplayGroup $group -Scope $Scope
    /// Author: Peter Cuttriss
    /// Creation Date: 8/10/2010
    /// </summary>
    [Cmdlet(VerbsCommon.Remove, "SPEnterpriseSearchQueryScopeDisplayGroupScope")]
    public class RemoveEnterpriseSearchQueryScopeDisplayGroupScope : SPRemoveCmdletBase<ScopeDisplayGroup>
    {
        [Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0), Alias(new string[] { "g" })]
        public ScopeDisplayGroupPipeBind ScopeDisplayGroup { get; set; }

        [Parameter(Mandatory = true, ValueFromPipeline = true), Alias(new string[] { "s" })]
        public ScopePipeBind Scope { get; set; }

        protected override void DeleteDataObject()
        {
            ScopeDisplayGroup scopeDisplayGroup = ScopeDisplayGroup.Read();
            Scope scope = Scope.Read();

            // remove the scope from the display group
            scopeDisplayGroup.Remove(scope);
            scopeDisplayGroup.Update();
        }
    }
}